3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
39 protected $fileStatus;
44 protected $revisionStatus;
46 function __construct( $title ) {
47 if( is_null( $title ) ) {
48 throw new MWException( __METHOD__
. ' given a null title.' );
50 $this->title
= $title;
54 * List all deleted pages recorded in the archive table. Returns result
55 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
58 * @return ResultWrapper
60 public static function listAllPages() {
61 $dbr = wfGetDB( DB_SLAVE
);
62 return self
::listPages( $dbr, '' );
66 * List deleted pages recorded in the archive table matching the
68 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
70 * @param string $prefix title prefix
71 * @return ResultWrapper
73 public static function listPagesByPrefix( $prefix ) {
74 $dbr = wfGetDB( DB_SLAVE
);
76 $title = Title
::newFromText( $prefix );
78 $ns = $title->getNamespace();
79 $prefix = $title->getDBkey();
81 // Prolly won't work too good
82 // @todo handle bare namespace names cleanly?
86 'ar_namespace' => $ns,
87 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
89 return self
::listPages( $dbr, $conds );
93 * @param $dbr DatabaseBase
95 * @return bool|ResultWrapper
97 protected static function listPages( $dbr, $condition ) {
98 return $dbr->resultObject(
104 'count' => 'COUNT(*)'
109 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
110 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
118 * List the revisions of the given page. Returns result wrapper with
119 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
121 * @return ResultWrapper
123 function listRevisions() {
124 global $wgContentHandlerUseDB;
126 $dbr = wfGetDB( DB_SLAVE
);
129 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
130 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
133 if ( $wgContentHandlerUseDB ) {
134 $fields[] = 'ar_content_format';
135 $fields[] = 'ar_content_model';
138 $res = $dbr->select( 'archive',
140 array( 'ar_namespace' => $this->title
->getNamespace(),
141 'ar_title' => $this->title
->getDBkey() ),
143 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
144 $ret = $dbr->resultObject( $res );
149 * List the deleted file revisions for this page, if it's a file page.
150 * Returns a result wrapper with various filearchive fields, or null
151 * if not a file page.
153 * @return ResultWrapper
154 * @todo Does this belong in Image for fuller encapsulation?
156 function listFiles() {
157 if( $this->title
->getNamespace() == NS_FILE
) {
158 $dbr = wfGetDB( DB_SLAVE
);
161 ArchivedFile
::selectFields(),
162 array( 'fa_name' => $this->title
->getDBkey() ),
164 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
165 $ret = $dbr->resultObject( $res );
172 * Return a Revision object containing data for the deleted revision.
173 * Note that the result *may* or *may not* have a null page ID.
175 * @param $timestamp String
178 function getRevision( $timestamp ) {
179 global $wgContentHandlerUseDB;
181 $dbr = wfGetDB( DB_SLAVE
);
198 if ( $wgContentHandlerUseDB ) {
199 $fields[] = 'ar_content_format';
200 $fields[] = 'ar_content_model';
203 $row = $dbr->selectRow( 'archive',
205 array( 'ar_namespace' => $this->title
->getNamespace(),
206 'ar_title' => $this->title
->getDBkey(),
207 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
210 return Revision
::newFromArchiveRow( $row, array( 'title' => $this->title
) );
217 * Return the most-previous revision, either live or deleted, against
218 * the deleted revision given by timestamp.
220 * May produce unexpected results in case of history merges or other
221 * unusual time issues.
223 * @param $timestamp String
224 * @return Revision or null
226 function getPreviousRevision( $timestamp ) {
227 $dbr = wfGetDB( DB_SLAVE
);
229 // Check the previous deleted revision...
230 $row = $dbr->selectRow( 'archive',
232 array( 'ar_namespace' => $this->title
->getNamespace(),
233 'ar_title' => $this->title
->getDBkey(),
235 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
238 'ORDER BY' => 'ar_timestamp DESC',
240 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
242 $row = $dbr->selectRow( array( 'page', 'revision' ),
243 array( 'rev_id', 'rev_timestamp' ),
245 'page_namespace' => $this->title
->getNamespace(),
246 'page_title' => $this->title
->getDBkey(),
247 'page_id = rev_page',
249 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
252 'ORDER BY' => 'rev_timestamp DESC',
254 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
255 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
257 if( $prevLive && $prevLive > $prevDeleted ) {
258 // Most prior revision was live
259 return Revision
::newFromId( $prevLiveId );
260 } elseif( $prevDeleted ) {
261 // Most prior revision was deleted
262 return $this->getRevision( $prevDeleted );
264 // No prior revision on this page.
270 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
272 * @param $row Object: database row
275 function getTextFromRow( $row ) {
276 if( is_null( $row->ar_text_id
) ) {
277 // An old row from MediaWiki 1.4 or previous.
278 // Text is embedded in this row in classic compression format.
279 return Revision
::getRevisionText( $row, 'ar_' );
281 // New-style: keyed to the text storage backend.
282 $dbr = wfGetDB( DB_SLAVE
);
283 $text = $dbr->selectRow( 'text',
284 array( 'old_text', 'old_flags' ),
285 array( 'old_id' => $row->ar_text_id
),
287 return Revision
::getRevisionText( $text );
292 * Fetch (and decompress if necessary) the stored text of the most
293 * recently edited deleted revision of the page.
295 * If there are no archived revisions for the page, returns NULL.
299 function getLastRevisionText() {
300 $dbr = wfGetDB( DB_SLAVE
);
301 $row = $dbr->selectRow( 'archive',
302 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
303 array( 'ar_namespace' => $this->title
->getNamespace(),
304 'ar_title' => $this->title
->getDBkey() ),
306 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
308 return $this->getTextFromRow( $row );
315 * Quick check if any archived revisions are present for the page.
319 function isDeleted() {
320 $dbr = wfGetDB( DB_SLAVE
);
321 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
322 array( 'ar_namespace' => $this->title
->getNamespace(),
323 'ar_title' => $this->title
->getDBkey() ),
330 * Restore the given (or all) text and file revisions for the page.
331 * Once restored, the items will be removed from the archive tables.
332 * The deletion log will be updated with an undeletion notice.
334 * @param array $timestamps pass an empty array to restore all revisions, otherwise list the ones to undelete.
335 * @param $comment String
336 * @param $fileVersions Array
337 * @param $unsuppress Boolean
338 * @param $user User doing the action, or null to use $wgUser
340 * @return array(number of file revisions restored, number of image revisions restored, log message)
341 * on success, false on failure
343 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
344 // If both the set of text revisions and file revisions are empty,
345 // restore everything. Otherwise, just restore the requested items.
346 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
348 $restoreText = $restoreAll ||
!empty( $timestamps );
349 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
351 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
352 $img = wfLocalFile( $this->title
);
353 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
354 if ( !$this->fileStatus
->isOK() ) {
357 $filesRestored = $this->fileStatus
->successCount
;
363 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
364 if( !$this->revisionStatus
->isOK() ) {
368 $textRestored = $this->revisionStatus
->getValue();
375 if( $textRestored && $filesRestored ) {
376 $reason = wfMessage( 'undeletedrevisions-files' )
377 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
378 } elseif( $textRestored ) {
379 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
380 ->inContentLanguage()->text();
381 } elseif( $filesRestored ) {
382 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
383 ->inContentLanguage()->text();
385 wfDebug( "Undelete: nothing undeleted...\n" );
389 if( trim( $comment ) != '' ) {
390 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
393 if ( $user === null ) {
398 $logEntry = new ManualLogEntry( 'delete', 'restore' );
399 $logEntry->setPerformer( $user );
400 $logEntry->setTarget( $this->title
);
401 $logEntry->setComment( $reason );
403 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
405 $logid = $logEntry->insert();
406 $logEntry->publish( $logid );
408 return array( $textRestored, $filesRestored, $reason );
412 * This is the meaty bit -- restores archived revisions of the given page
413 * to the cur/old tables. If the page currently exists, all revisions will
414 * be stuffed into old, otherwise the most recent will go into cur.
416 * @param array $timestamps pass an empty array to restore all revisions, otherwise list the ones to undelete.
417 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
419 * @param $comment String
420 * @throws ReadOnlyError
421 * @return Status, containing the number of revisions restored on success
423 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
424 global $wgContentHandlerUseDB;
426 if ( wfReadOnly() ) {
427 throw new ReadOnlyError();
429 $restoreAll = empty( $timestamps );
431 $dbw = wfGetDB( DB_MASTER
);
433 # Does this page already exist? We'll have to update it...
434 $article = WikiPage
::factory( $this->title
);
435 # Load latest data for the current page (bug 31179)
436 $article->loadPageData( 'fromdbmaster' );
437 $oldcountable = $article->isCountable();
439 $page = $dbw->selectRow( 'page',
440 array( 'page_id', 'page_latest' ),
441 array( 'page_namespace' => $this->title
->getNamespace(),
442 'page_title' => $this->title
->getDBkey() ),
444 array( 'FOR UPDATE' ) // lock page
448 # Page already exists. Import the history, and if necessary
449 # we'll update the latest revision field in the record.
451 $previousRevId = $page->page_latest
;
453 # Get the time span of this page
454 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
455 array( 'rev_id' => $previousRevId ),
458 if( $previousTimestamp === false ) {
459 wfDebug( __METHOD__
. ": existing page refers to a page_latest that does not exist\n" );
461 $status = Status
::newGood( 0 );
462 $status->warning( 'undeleterevision-missing' );
467 # Have to create a new article...
470 $previousTimestamp = 0;
474 $oldones = '1 = 1'; # All revisions...
476 $oldts = implode( ',',
477 array_map( array( &$dbw, 'addQuotes' ),
478 array_map( array( &$dbw, 'timestamp' ),
481 $oldones = "ar_timestamp IN ( {$oldts} )";
500 if ( $wgContentHandlerUseDB ) {
501 $fields[] = 'ar_content_format';
502 $fields[] = 'ar_content_model';
506 * Select each archived revision...
508 $result = $dbw->select( 'archive',
511 'ar_namespace' => $this->title
->getNamespace(),
512 'ar_title' => $this->title
->getDBkey(),
515 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
517 $ret = $dbw->resultObject( $result );
518 $rev_count = $dbw->numRows( $result );
520 wfDebug( __METHOD__
. ": no revisions to restore\n" );
522 $status = Status
::newGood( 0 );
523 $status->warning( "undelete-no-results" );
527 $ret->seek( $rev_count - 1 ); // move to last
528 $row = $ret->fetchObject(); // get newest archived rev
529 $ret->seek( 0 ); // move back
531 // grab the content to check consistency with global state before restoring the page.
532 $revision = Revision
::newFromArchiveRow( $row,
534 'title' => $article->getTitle(), // used to derive default content model
537 $user = User
::newFromName( $revision->getRawUserText(), false );
538 $content = $revision->getContent( Revision
::RAW
);
540 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
541 $status = $content->prepareSave( $article, 0, -1, $user );
543 if ( !$status->isOK() ) {
548 // Check the state of the newest to-be version...
549 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
550 return Status
::newFatal( "undeleterevdel" );
552 // Safe to insert now...
553 $newid = $article->insertOn( $dbw );
556 // Check if a deleted revision will become the current revision...
557 if( $row->ar_timestamp
> $previousTimestamp ) {
558 // Check the state of the newest to-be version...
559 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
560 return Status
::newFatal( "undeleterevdel" );
565 $pageId = $article->getId();
571 foreach ( $ret as $row ) {
572 // Check for key dupes due to shitty archive integrity.
573 if( $row->ar_rev_id
) {
574 $exists = $dbw->selectField( 'revision', '1',
575 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
577 continue; // don't throw DB errors
580 // Insert one revision at a time...maintaining deletion status
581 // unless we are specifically removing all restrictions...
582 $revision = Revision
::newFromArchiveRow( $row,
585 'title' => $this->title
,
586 'deleted' => $unsuppress ?
0 : $row->ar_deleted
589 $revision->insertOn( $dbw );
592 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
594 # Now that it's safely stored, take it out of the archive
595 $dbw->delete( 'archive',
597 'ar_namespace' => $this->title
->getNamespace(),
598 'ar_title' => $this->title
->getDBkey(),
602 // Was anything restored at all?
603 if ( $restored == 0 ) {
604 return Status
::newGood( 0 );
607 $created = (bool)$newid;
609 // Attach the latest revision to the page...
610 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
611 if ( $created ||
$wasnew ) {
612 // Update site stats, link tables, etc
613 $user = User
::newFromName( $revision->getRawUserText(), false );
614 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
617 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
619 if( $this->title
->getNamespace() == NS_FILE
) {
620 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
624 return Status
::newGood( $restored );
630 function getFileStatus() { return $this->fileStatus
; }
635 function getRevisionStatus() { return $this->revisionStatus
; }
639 * Special page allowing users with the appropriate permissions to view
640 * and restore deleted content.
642 * @ingroup SpecialPage
644 class SpecialUndelete
extends SpecialPage
{
645 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
646 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
653 function __construct() {
654 parent
::__construct( 'Undelete', 'deletedhistory' );
657 function loadRequest( $par ) {
658 $request = $this->getRequest();
659 $user = $this->getUser();
661 $this->mAction
= $request->getVal( 'action' );
662 if ( $par !== null && $par !== '' ) {
663 $this->mTarget
= $par;
665 $this->mTarget
= $request->getVal( 'target' );
667 $this->mTargetObj
= null;
668 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
669 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
671 $this->mSearchPrefix
= $request->getText( 'prefix' );
672 $time = $request->getVal( 'timestamp' );
673 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
674 $this->mFilename
= $request->getVal( 'file' );
676 $posted = $request->wasPosted() &&
677 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
678 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
679 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
680 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
681 $this->mDiff
= $request->getCheck( 'diff' );
682 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
683 $this->mComment
= $request->getText( 'wpComment' );
684 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
685 $this->mToken
= $request->getVal( 'token' );
687 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
688 $this->mAllowed
= true; // user can restore
689 $this->mCanView
= true; // user can view content
690 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
691 $this->mAllowed
= false; // user cannot restore
692 $this->mCanView
= true; // user can view content
693 $this->mRestore
= false;
694 } else { // user can only view the list of revisions
695 $this->mAllowed
= false;
696 $this->mCanView
= false;
697 $this->mTimestamp
= '';
698 $this->mRestore
= false;
701 if( $this->mRestore ||
$this->mInvert
) {
702 $timestamps = array();
703 $this->mFileVersions
= array();
704 foreach( $request->getValues() as $key => $val ) {
706 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
707 array_push( $timestamps, $matches[1] );
710 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
711 $this->mFileVersions
[] = intval( $matches[1] );
714 rsort( $timestamps );
715 $this->mTargetTimestamp
= $timestamps;
719 function execute( $par ) {
720 $this->checkPermissions();
721 $user = $this->getUser();
724 $this->outputHeader();
726 $this->loadRequest( $par );
728 $out = $this->getOutput();
730 if ( is_null( $this->mTargetObj
) ) {
731 $out->addWikiMsg( 'undelete-header' );
733 # Not all users can just browse every deleted page from the list
734 if ( $user->isAllowed( 'browsearchive' ) ) {
735 $this->showSearchForm();
740 if ( $this->mAllowed
) {
741 $out->setPageTitle( $this->msg( 'undeletepage' ) );
743 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
746 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
748 if ( $this->mTimestamp
!== '' ) {
749 $this->showRevision( $this->mTimestamp
);
750 } elseif ( $this->mFilename
!== null ) {
751 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
752 // Check if user is allowed to see this file
753 if ( !$file->exists() ) {
754 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
755 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
756 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
757 throw new PermissionsError( 'suppressrevision' );
759 throw new PermissionsError( 'deletedtext' );
761 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
762 $this->showFileConfirmationForm( $this->mFilename
);
764 $this->showFile( $this->mFilename
);
766 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
769 $this->showHistory();
773 function showSearchForm() {
776 $out = $this->getOutput();
777 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
779 Xml
::openElement( 'form', array(
781 'action' => $wgScript ) ) .
782 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
783 Html
::hidden( 'title',
784 $this->getTitle()->getPrefixedDBkey() ) .
785 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
786 'prefix', 'prefix', 20,
787 $this->mSearchPrefix
) . ' ' .
788 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
789 Xml
::closeElement( 'fieldset' ) .
790 Xml
::closeElement( 'form' )
793 # List undeletable articles
794 if( $this->mSearchPrefix
) {
795 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
796 $this->showList( $result );
801 * Generic list of deleted pages
803 * @param $result ResultWrapper
806 private function showList( $result ) {
807 $out = $this->getOutput();
809 if( $result->numRows() == 0 ) {
810 $out->addWikiMsg( 'undelete-no-results' );
814 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
816 $undelete = $this->getTitle();
817 $out->addHTML( "<ul>\n" );
818 foreach ( $result as $row ) {
819 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
820 if ( $title !== null ) {
821 $item = Linker
::linkKnown(
823 htmlspecialchars( $title->getPrefixedText() ),
825 array( 'target' => $title->getPrefixedText() )
828 // The title is no longer valid, show as text
829 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
830 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
832 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
833 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
836 $out->addHTML( "</ul>\n" );
841 private function showRevision( $timestamp ) {
842 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
846 $archive = new PageArchive( $this->mTargetObj
);
847 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) ) ) {
850 $rev = $archive->getRevision( $timestamp );
852 $out = $this->getOutput();
853 $user = $this->getUser();
856 $out->addWikiMsg( 'undeleterevision-missing' );
860 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
861 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
862 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
865 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
866 $out->addHTML( '<br />' );
867 // and we are allowed to see...
872 $previousRev = $archive->getPreviousRevision( $timestamp );
874 $this->showDiff( $previousRev, $rev );
875 if( $this->mDiffOnly
) {
878 $out->addHTML( '<hr />' );
881 $out->addWikiMsg( 'undelete-nodiff' );
885 $link = Linker
::linkKnown(
886 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
887 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
890 $lang = $this->getLanguage();
892 // date and time are separate parameters to facilitate localisation.
893 // $time is kept for backward compat reasons.
894 $time = $lang->userTimeAndDate( $timestamp, $user );
895 $d = $lang->userDate( $timestamp, $user );
896 $t = $lang->userTime( $timestamp, $user );
897 $userLink = Linker
::revUserTools( $rev );
899 $content = $rev->getContent( Revision
::FOR_THIS_USER
, $user );
901 $isText = ( $content instanceof TextContent
);
903 if( $this->mPreview ||
$isText ) {
904 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
906 $openDiv = '<div id="mw-undelete-revision">';
908 $out->addHTML( $openDiv );
910 // Revision delete links
911 if ( !$this->mDiff
) {
912 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
914 $out->addHTML( "$revdel " );
918 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
919 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
921 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) ) ) {
925 if( $this->mPreview ||
!$isText ) {
926 // NOTE: non-text content has no source view, so always use rendered preview
929 $popts = $out->parserOptions();
930 $popts->setEditSection( false );
932 $pout = $content->getParserOutput( $this->mTargetObj
, $rev->getId(), $popts, true );
933 $out->addParserOutput( $pout );
937 // source view for textual content
938 $sourceView = Xml
::element( 'textarea', array(
939 'readonly' => 'readonly',
940 'cols' => $user->getIntOption( 'cols' ),
941 'rows' => $user->getIntOption( 'rows' ) ),
942 $content->getNativeData() . "\n" );
944 $previewButton = Xml
::element( 'input', array(
947 'value' => $this->msg( 'showpreview' )->text() ) );
953 $diffButton = Xml
::element( 'input', array(
956 'value' => $this->msg( 'showdiff' )->text() ) );
960 Xml
::openElement( 'div', array(
961 'style' => 'clear: both' ) ) .
962 Xml
::openElement( 'form', array(
964 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
965 Xml
::element( 'input', array(
968 'value' => $this->mTargetObj
->getPrefixedDBkey() ) ) .
969 Xml
::element( 'input', array(
971 'name' => 'timestamp',
972 'value' => $timestamp ) ) .
973 Xml
::element( 'input', array(
975 'name' => 'wpEditToken',
976 'value' => $user->getEditToken() ) ) .
979 Xml
::closeElement( 'form' ) .
980 Xml
::closeElement( 'div' ) );
984 * Build a diff display between this and the previous either deleted
985 * or non-deleted edit.
987 * @param $previousRev Revision
988 * @param $currentRev Revision
989 * @return String: HTML
991 function showDiff( $previousRev, $currentRev ) {
992 $diffContext = clone $this->getContext();
993 $diffContext->setTitle( $currentRev->getTitle() );
994 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
996 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
997 $diffEngine->showDiffStyle();
998 $this->getOutput()->addHTML(
1000 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1001 "<col class='diff-marker' />" .
1002 "<col class='diff-content' />" .
1003 "<col class='diff-marker' />" .
1004 "<col class='diff-content' />" .
1006 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1007 $this->diffHeader( $previousRev, 'o' ) .
1009 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1010 $this->diffHeader( $currentRev, 'n' ) .
1013 $diffEngine->generateContentDiffBody(
1014 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
1015 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1022 * @param $rev Revision
1026 private function diffHeader( $rev, $prefix ) {
1027 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1029 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1030 $targetPage = $this->getTitle();
1031 $targetQuery = array(
1032 'target' => $this->mTargetObj
->getPrefixedText(),
1033 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1036 /// @todo FIXME: getId() may return non-zero for deleted revs...
1037 $targetPage = $rev->getTitle();
1038 $targetQuery = array( 'oldid' => $rev->getId() );
1040 // Add show/hide deletion links if available
1041 $user = $this->getUser();
1042 $lang = $this->getLanguage();
1043 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1044 if ( $rdel ) $rdel = " $rdel";
1046 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1051 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1052 $lang->userDate( $rev->getTimestamp(), $user ),
1053 $lang->userTime( $rev->getTimestamp(), $user )
1059 '<div id="mw-diff-' . $prefix . 'title2">' .
1060 Linker
::revUserTools( $rev ) . '<br />' .
1062 '<div id="mw-diff-' . $prefix . 'title3">' .
1063 Linker
::revComment( $rev ) . $rdel . '<br />' .
1068 * Show a form confirming whether a tokenless user really wants to see a file
1070 private function showFileConfirmationForm( $key ) {
1071 $out = $this->getOutput();
1072 $lang = $this->getLanguage();
1073 $user = $this->getUser();
1074 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1075 $out->addWikiMsg( 'undelete-show-file-confirm',
1076 $this->mTargetObj
->getText(),
1077 $lang->userDate( $file->getTimestamp(), $user ),
1078 $lang->userTime( $file->getTimestamp(), $user ) );
1080 Xml
::openElement( 'form', array(
1082 'action' => $this->getTitle()->getLocalURL(
1083 'target=' . urlencode( $this->mTarget
) .
1084 '&file=' . urlencode( $key ) .
1085 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1088 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1094 * Show a deleted file version requested by the visitor.
1096 private function showFile( $key ) {
1097 $this->getOutput()->disable();
1099 # We mustn't allow the output to be Squid cached, otherwise
1100 # if an admin previews a deleted image, and it's cached, then
1101 # a user without appropriate permissions can toddle off and
1102 # nab the image, and Squid will serve it
1103 $response = $this->getRequest()->response();
1104 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1105 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1106 $response->header( 'Pragma: no-cache' );
1108 $repo = RepoGroup
::singleton()->getLocalRepo();
1109 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1110 $repo->streamFile( $path );
1113 private function showHistory() {
1114 $out = $this->getOutput();
1115 if( $this->mAllowed
) {
1116 $out->addModules( 'mediawiki.special.undelete' );
1119 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1120 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1123 $archive = new PageArchive( $this->mTargetObj
);
1124 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1126 $text = $archive->getLastRevisionText();
1127 if( is_null( $text ) ) {
1128 $out->addWikiMsg( 'nohistory' );
1132 $out->addHTML( '<div class="mw-undelete-history">' );
1133 if ( $this->mAllowed
) {
1134 $out->addWikiMsg( 'undeletehistory' );
1135 $out->addWikiMsg( 'undeleterevdel' );
1137 $out->addWikiMsg( 'undeletehistorynoadmin' );
1139 $out->addHTML( '</div>' );
1141 # List all stored revisions
1142 $revisions = $archive->listRevisions();
1143 $files = $archive->listFiles();
1145 $haveRevisions = $revisions && $revisions->numRows() > 0;
1146 $haveFiles = $files && $files->numRows() > 0;
1148 # Batch existence check on user and talk pages
1149 if( $haveRevisions ) {
1150 $batch = new LinkBatch();
1151 foreach ( $revisions as $row ) {
1152 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1153 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1156 $revisions->seek( 0 );
1159 $batch = new LinkBatch();
1160 foreach ( $files as $row ) {
1161 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1162 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1168 if ( $this->mAllowed
) {
1169 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1170 # Start the form here
1171 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1172 $out->addHTML( $top );
1175 # Show relevant lines from the deletion log:
1176 $deleteLogPage = new LogPage( 'delete' );
1177 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1178 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1179 # Show relevant lines from the suppression log:
1180 $suppressLogPage = new LogPage( 'suppress' );
1181 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1182 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1183 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1186 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1187 # Format the user-visible controls (comment field, submission button)
1188 # in a nice little table
1189 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1193 <td class='mw-input'>" .
1194 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1195 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1199 $unsuppressBox = '';
1202 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1203 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1205 <td colspan='2' class='mw-undelete-extrahelp'>" .
1206 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1210 <td class='mw-label'>" .
1211 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1213 <td class='mw-input'>" .
1214 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1219 <td class='mw-submit'>" .
1220 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1221 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1225 Xml
::closeElement( 'table' ) .
1226 Xml
::closeElement( 'fieldset' );
1228 $out->addHTML( $table );
1231 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1233 if( $haveRevisions ) {
1234 # The page's stored (deleted) history:
1235 $out->addHTML( '<ul>' );
1236 $remaining = $revisions->numRows();
1237 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1239 foreach ( $revisions as $row ) {
1241 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1244 $out->addHTML( '</ul>' );
1246 $out->addWikiMsg( 'nohistory' );
1250 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1251 $out->addHTML( '<ul>' );
1252 foreach ( $files as $row ) {
1253 $out->addHTML( $this->formatFileRow( $row ) );
1256 $out->addHTML( '</ul>' );
1259 if ( $this->mAllowed
) {
1260 # Slip in the hidden controls here
1261 $misc = Html
::hidden( 'target', $this->mTarget
);
1262 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1263 $misc .= Xml
::closeElement( 'form' );
1264 $out->addHTML( $misc );
1270 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1271 $rev = Revision
::newFromArchiveRow( $row,
1273 'title' => $this->mTargetObj
1277 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1278 // Build checkboxen...
1279 if( $this->mAllowed
) {
1280 if( $this->mInvert
) {
1281 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1282 $checkBox = Xml
::check( "ts$ts" );
1284 $checkBox = Xml
::check( "ts$ts", true );
1287 $checkBox = Xml
::check( "ts$ts" );
1292 $user = $this->getUser();
1293 // Build page & diff links...
1294 if( $this->mCanView
) {
1295 $titleObj = $this->getTitle();
1297 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1298 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1299 $last = $this->msg( 'diff' )->escaped();
1300 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1301 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1302 $last = Linker
::linkKnown(
1304 $this->msg( 'diff' )->escaped(),
1307 'target' => $this->mTargetObj
->getPrefixedText(),
1313 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1314 $last = $this->msg( 'diff' )->escaped();
1317 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1318 $last = $this->msg( 'diff' )->escaped();
1321 $userLink = Linker
::revUserTools( $rev );
1322 // Revision text size
1323 $size = $row->ar_len
;
1324 if( !is_null( $size ) ) {
1325 $revTextSize = Linker
::formatRevisionSize( $size );
1328 $comment = Linker
::revComment( $rev );
1329 // Revision delete links
1330 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1332 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink, $userLink, $revTextSize, $comment )->escaped();
1333 return "<li>$revisionRow</li>";
1336 private function formatFileRow( $row ) {
1337 $file = ArchivedFile
::newFromRow( $row );
1339 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1340 $user = $this->getUser();
1341 if( $this->mAllowed
&& $row->fa_storage_key
) {
1342 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1343 $key = urlencode( $row->fa_storage_key
);
1344 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1347 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1349 $userLink = $this->getFileUser( $file );
1350 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1351 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1352 $data = htmlspecialchars( $data . ' ' . $bytes );
1353 $comment = $this->getFileComment( $file );
1355 // Add show/hide deletion links if available
1356 $canHide = $user->isAllowed( 'deleterevision' );
1357 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1358 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1359 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1362 'type' => 'filearchive',
1363 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1364 'ids' => $row->fa_id
1366 $revdlink = Linker
::revDeleteLink( $query,
1367 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1373 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1377 * Fetch revision text link if it's available to all users
1379 * @param $rev Revision
1380 * @param $titleObj Title
1381 * @param string $ts Timestamp
1384 function getPageLink( $rev, $titleObj, $ts ) {
1385 $user = $this->getUser();
1386 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1388 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1389 return '<span class="history-deleted">' . $time . '</span>';
1391 $link = Linker
::linkKnown(
1393 htmlspecialchars( $time ),
1396 'target' => $this->mTargetObj
->getPrefixedText(),
1400 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1401 $link = '<span class="history-deleted">' . $link . '</span>';
1408 * Fetch image view link if it's available to all users
1411 * @param $titleObj Title
1412 * @param string $ts A timestamp
1413 * @param string $key a storage key
1415 * @return String: HTML fragment
1417 function getFileLink( $file, $titleObj, $ts, $key ) {
1418 $user = $this->getUser();
1419 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1421 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1422 return '<span class="history-deleted">' . $time . '</span>';
1424 $link = Linker
::linkKnown(
1426 htmlspecialchars( $time ),
1429 'target' => $this->mTargetObj
->getPrefixedText(),
1431 'token' => $user->getEditToken( $key )
1434 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1435 $link = '<span class="history-deleted">' . $link . '</span>';
1442 * Fetch file's user id if it's available to this user
1445 * @return String: HTML fragment
1447 function getFileUser( $file ) {
1448 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1449 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1451 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1452 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1453 if( $file->isDeleted( File
::DELETED_USER
) ) {
1454 $link = '<span class="history-deleted">' . $link . '</span>';
1461 * Fetch file upload comment if it's available to this user
1464 * @return String: HTML fragment
1466 function getFileComment( $file ) {
1467 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1468 return '<span class="history-deleted"><span class="comment">' .
1469 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1471 $link = Linker
::commentBlock( $file->getRawDescription() );
1472 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1473 $link = '<span class="history-deleted">' . $link . '</span>';
1479 function undelete() {
1480 global $wgUploadMaintenance;
1482 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1483 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1486 if ( wfReadOnly() ) {
1487 throw new ReadOnlyError
;
1490 $out = $this->getOutput();
1491 $archive = new PageArchive( $this->mTargetObj
);
1492 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1493 $ok = $archive->undelete(
1494 $this->mTargetTimestamp
,
1496 $this->mFileVersions
,
1501 if( is_array( $ok ) ) {
1502 if ( $ok[1] ) { // Undeleted file count
1503 wfRunHooks( 'FileUndeleteComplete', array(
1504 $this->mTargetObj
, $this->mFileVersions
,
1505 $this->getUser(), $this->mComment
) );
1508 $link = Linker
::linkKnown( $this->mTargetObj
);
1509 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1511 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1514 // Show revision undeletion warnings and errors
1515 $status = $archive->getRevisionStatus();
1516 if( $status && !$status->isGood() ) {
1517 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1520 // Show file undeletion warnings and errors
1521 $status = $archive->getFileStatus();
1522 if( $status && !$status->isGood() ) {
1523 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1527 protected function getGroupName() {